using System;
using System.Collections.Generic;
using iPhOwa.Exchange.ContentClasses;
using iPhOwa.Exchange.ContentClasses.Fields;
using iPhOwa.Exchange.WebDav;
using iPhOwa.Exchange.WebDav.Methods;

namespace iPhOwa.Exchange.Managers
{
	/// <summary>
	/// Encapsulates working with asppointments.
	/// </summary>
	public static class AppointmentManager
	{
		/// <summary>
		/// Get a list of appointments.
		/// </summary>
		public static ICollection<Appointment> GetList(Uri path, OwaSession session)
		{
			string[] properties = new string[]
					{
							ItemFields.Href
							, MessageFields.NormalizedSubject
							, AppointmentFields.AllDayEvent
							, AppointmentFields.DateStart
							, AppointmentFields.DateEnd
							, AppointmentFields.Location
					};

			Search search = new Search(path, session, properties);
			search.OrderBys.Add(new OrderBy(AppointmentFields.DateStart));
			search.Wheres.Add(new Where(AppointmentFields.DateEnd, ConditionalOperator.GreaterThan, DateTime.Now.Subtract(DateTime.Now.TimeOfDay)));

			List<Item> items = new List<Item>(search.Run());

			return items.ConvertAll<Appointment>(delegate(Item i)
			                                     	{
			                                     		return i as Appointment;
			                                     	});
		}

		/// <summary>
		/// Get a specific appointment.
		/// </summary>
		public static Appointment Get(Uri path, OwaSession session)
		{
			string[] properties = new string[]
					{
							MessageFields.HtmlDescription
							, ItemFields.Href
							, MessageFields.NormalizedSubject
							, AppointmentFields.AllDayEvent
							, AppointmentFields.DateStart
							, AppointmentFields.DateEnd
							, AppointmentFields.Location
							, ItemFields.ParentName
					};

			PropFind propFind = new PropFind(path, session, properties);

			return (Appointment)propFind.Run();
		}
	}
}